package com.vvv.shortlink.project.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vvv.shortlink.common.convention.result.Result;
import com.vvv.shortlink.common.convention.result.Results;
import com.vvv.shortlink.common.toolkit.UserContext;
import com.vvv.shortlink.project.dto.req.LinkBatchCreateReqDTO;
import com.vvv.shortlink.project.dto.req.LinkCreateReqDTO;
import com.vvv.shortlink.project.dto.req.LinkPageReqDTO;
import com.vvv.shortlink.project.dto.req.LinkUpdateReqDTO;
import com.vvv.shortlink.project.dto.resp.LinkBatchCreateRespDTO;
import com.vvv.shortlink.project.dto.resp.LinkCreateRespDTO;
import com.vvv.shortlink.project.dto.resp.LinkGroupCountRespDTO;
import com.vvv.shortlink.project.dto.resp.LinkPageRespDTO;
import com.vvv.shortlink.project.handler.CustomBlockHandler;
import com.vvv.shortlink.project.service.LinkService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author vvv
 * @date 2024/2/2 10 24
 * discription
 */
@RestController
@RequiredArgsConstructor
public class LinkController {

    private final LinkService linkService;

    /**
     * 短链接跳转原始链接
     */
    @GetMapping("/{short-uri}")
    public void restoreUrl(@PathVariable("short-uri") String shortUri, HttpServletRequest request, HttpServletResponse response) {
        linkService.restoreUrl(shortUri, request, response);
    }

    /**
     * 更新管链接
     */
    @PostMapping("/api/short-link/v1/update")
    public Result<Void> updateShortLink(@RequestBody LinkUpdateReqDTO requestParam) {
        if (requestParam.getUserId() == null) {
            requestParam.setUserId(UserContext.getUserId());
        }
        linkService.updateShortLink(requestParam);
        return Results.success();
    }

    /**
     * 分页查询短链接
     */
    @GetMapping("/api/short-link/v1/page")
    public Result<IPage<LinkPageRespDTO>> pageShortLink(LinkPageReqDTO requestParam) {
        if (requestParam.getUserId() == null) {
            requestParam.setUserId(UserContext.getUserId());
        }
        return Results.success(linkService.pageShortLink(requestParam));
    }

    /**
     * 创建短链接
     */
    @PostMapping("/api/short-link/v1/create")
    @SentinelResource(
            value = "create_short-link",
            blockHandler = "createShortLinkBlockHandlerMethod",
            blockHandlerClass = CustomBlockHandler.class
    )
    public Result<LinkCreateRespDTO> createShortLink(@RequestBody LinkCreateReqDTO requestParam) {
        if (requestParam.getUserId() == null) {
            requestParam.setUserId(UserContext.getUserId());
        }
        return Results.success(linkService.createShortLink(requestParam));
    }

    /**
     * 批量创建短链接
     */
    @PostMapping("/api/short-link/v1/create/batch")
    public Result<LinkBatchCreateRespDTO> batchCreateShortLink(@RequestBody LinkBatchCreateReqDTO requestParam) {
        if (requestParam.getUserId() == null) {
            requestParam.setUserId(UserContext.getUserId());
        }
        return Results.success(linkService.batchCreateShortLink(requestParam));
    }

    /**
     * 统计userId下面的个分组下面的短链接数量
     */
    @GetMapping("/api/short-link/v1/count")
    public Result<List<LinkGroupCountRespDTO>> listShortLinkGroupCount(@RequestParam("userId") Long userId) {
        return Results.success(linkService.listShortLinkGroupCount(userId));
    }
}
